package com.yiren.backstage.judge.services.light.impl;

import android.content.Context;

import com.yiren.backstage.currency.entity.GradeStandardEntity;
import com.yiren.backstage.currency.entity.LightCombinationEntity;
import com.yiren.backstage.currency.entity.ProjectEntity;
import com.yiren.backstage.currency.enums.TaskStateEnum;
import com.yiren.backstage.db.dao.cache.LightCombinationCache;
import com.yiren.backstage.judge.api.IExamStateCallBack;
import com.yiren.backstage.judge.api.impl.ExamJudgeApiImpl;
import com.yiren.backstage.judge.handler.LightJudgeHandler;
import com.yiren.backstage.judge.services.light.ILightJudgeService;
import com.yiren.backstage.judge.thread.CachedThreadPoolManage;
import com.yiren.backstage.judge.thread.LightJudgeThread;
import com.yiren.backstage.judge.thread.LightTipsThread;

/**
 * Time:2022/3/23 16:02
 * Author:xiaozan
 * Description:灯光自动评判 服务实现
 */
public class LightJudgeServiceImpl implements ILightJudgeService {

    private Context context;

    private LightJudgeHandler lightJudgeHandler;

    private IExamStateCallBack iExamStateCallBack;

    public LightJudgeServiceImpl(Context context, LightJudgeHandler lightJudgeHandler, IExamStateCallBack iExamStateCallBack) {
        this.context = context;
        this.lightJudgeHandler = lightJudgeHandler;
        this.iExamStateCallBack = iExamStateCallBack;
    }

    @Override
    public void startUpLightTips(ProjectEntity projectEntity) {
        //获取灯光组合
        LightCombinationEntity lightCombinationEntity = LightCombinationCache.getLightCombination(projectEntity.getId());
        if (null == lightCombinationEntity || null == lightCombinationEntity.getSingleLightEntities() || lightCombinationEntity.getSingleLightEntities().size() == 0) {
            projectEntity.setTaskState(TaskStateEnum.FINISH);
            ExamJudgeApiImpl.projectStateMap.put(projectEntity.getId(), projectEntity);
            //将此灯光状态该成已完成状态
            iExamStateCallBack.projectState(projectEntity, TaskStateEnum.FINISH);
            return;
        }
        //启动灯光操作前提示线程
        LightTipsThread lightTipsThread = new LightTipsThread(context, lightJudgeHandler, projectEntity);
        CachedThreadPoolManage.getInstance().startThread(lightTipsThread);
    }

    @Override
    public void startUpLightJudge(ProjectEntity projectEntity) {
        LightJudgeThread lightJudgeThread = new LightJudgeThread(context, lightJudgeHandler, projectEntity);
        CachedThreadPoolManage.getInstance().startThread(lightJudgeThread);
    }

    @Override
    public void addGradeStandard(GradeStandardEntity gradeStandardEntity) {
        iExamStateCallBack.addGradeStandard(gradeStandardEntity);
    }

    @Override
    public void lightProjectFinish(ProjectEntity projectEntity) {
        projectEntity.setTaskState(TaskStateEnum.FINISH);
        ExamJudgeApiImpl.projectStateMap.put(projectEntity.getId(), projectEntity);
        iExamStateCallBack.projectState(projectEntity, TaskStateEnum.FINISH);
    }
}
